#region License
///License Begin
///This program has been developed by: Rodolfo Grave de Peralta, 
///Copyright (C) 2007 Rodolfo Grave de Peralta
///This program is free software; you can redistribute it and/or modify
///it under the terms of the GNU General Public License as published by
///the Free Software Foundation; either version 2 of the License, or
///(at your option) any later version.
///
///This program is distributed in the hope that it will be useful,
///but WITHOUT ANY WARRANTY; without even the implied warranty of
///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///GNU General Public License for more details.
///
///You should have received a copy of the GNU General Public License
///along with this program; if not, write to the Free Software
///Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
///License End
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;

using TEAM.TimeTracking.Model;


namespace TEAM.TimeTracking.View
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class WidgetWindow : System.Windows.Window
    {

        protected System.Windows.Forms.NotifyIcon notifyIcon;
        protected System.Windows.Forms.MenuItem notifyShowHideMenuItem;
        protected ICollectionView currentProjectsView;


        public WidgetWindow()
        {
            InitializeComponent();

            notifyIcon = new System.Windows.Forms.NotifyIcon();
            notifyIcon.Text = "TEAM's TimeTracker";
            notifyIcon.Icon = new System.Drawing.Icon("TimeTracker.ico");
            notifyIcon.Visible = true;
            notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu();
            notifyIcon.ContextMenu.MenuItems.Add("Reporter", new EventHandler(Reporter_NotifyMenuItemClick));
            notifyIcon.ContextMenu.MenuItems.Add("Admin Tasks", new EventHandler(Admin_NotifyMenuItemClick));
            notifyIcon.ContextMenu.MenuItems.Add("Settings", new EventHandler(Settings_NotifyMenuItemClick));
            notifyShowHideMenuItem = new System.Windows.Forms.MenuItem("Hide", new EventHandler(ShowHide_NotifyMenuItemClick));
            notifyIcon.ContextMenu.MenuItems.Add(notifyShowHideMenuItem);
            notifyIcon.ContextMenu.MenuItems.Add("About TimeTracker", new EventHandler(About_NotifyMenuItemClick));
            notifyIcon.ContextMenu.MenuItems.Add("Exit", new EventHandler(Exit_NotifyMenuItemClick));
            notifyIcon.BalloonTipTitle = "TEAM's TimeTracker";
            notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
            notifyIcon.MouseClick +=
                delegate(object sender, System.Windows.Forms.MouseEventArgs e)
                {
                    if (e.Button == System.Windows.Forms.MouseButtons.Right)
                    {
                        this.ContextMenu.Visibility = Visibility.Visible;
                    }
                };
            notifyIcon.DoubleClick +=
                delegate(object sender, EventArgs args)
                {
                    ToggleVisibility();
                };

            currentProjectsView = CollectionViewSource.GetDefaultView(App.CurrentApp.CurrentModel.CurrentUser.Projects);
            UpdateCurrentProjectsView();
            ctrlProjects.ItemsSource = currentProjectsView;
            
            App.CurrentApp.CurrentModel.CurrentUser.TaskExecutionStarted += new User.TaskExecutionStartedEventHandler(currentUser_TaskExecutionStarted);
        }

        public void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        protected void Window_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            App.CurrentApp.ShowReportWindow();
        }

        protected void Window_Closing(object sender, CancelEventArgs e)
        {
            App.CurrentApp.GracefulShutDown();
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }

        protected void Window_Activated(object sender, EventArgs e)
        {
            UpdateOpacity();
        }

        protected void Window_Deactivated(object sender, EventArgs e)
        {
            UpdateOpacity();
        }

        protected void Window_KeyDown(object sender, KeyboardEventArgs e)
        {
            if (e.KeyboardDevice.IsKeyDown(Key.Escape))
            {
                Hide();
            }
        }

        protected void ctrlChangeTaskExpander_Collapsed(object sender, RoutedEventArgs e)
        {
            ctrlChangeTaskExpander.IsExpanded = false;
            UpdateOpacity();
        }

        protected void ctrlChangeTaskExpander_Expanded(object sender, RoutedEventArgs e)
        {
            ctrlChangeTaskExpander.IsExpanded = true;
            UpdateOpacity();
        }

        public void UpdateOpacity()
        {
            Opacity = (IsActive && ctrlChangeTaskExpander.IsExpanded) ? 1 : 0.7;
        }

        public void Reporter_NotifyMenuItemClick(object sender, EventArgs e)
        {
            App.CurrentApp.ShowReportWindow();
        }

        public void Admin_NotifyMenuItemClick(object sender, EventArgs e)
        {
            App.CurrentApp.ShowAdminWindow();
        }

        public void Settings_NotifyMenuItemClick(object sender, EventArgs e)
        {
            App.CurrentApp.ShowSettingsWindow();
        }

        public void About_NotifyMenuItemClick(object sender, EventArgs e)
        {
            App.CurrentApp.ShowAboutWindow();
        }

        protected void Exit_NotifyMenuItemClick(object sender, EventArgs e)
        {
            App.CurrentApp.GracefulShutDown();
        }

        protected void ToggleVisibility()
        {
            if (IsVisible)
            {
                Hide();
                notifyShowHideMenuItem.Text = "Show";
            }
            else
            {
                Show();
                notifyShowHideMenuItem.Text = "Hide";
            }
            if (!string.IsNullOrEmpty(notifyIcon.BalloonTipText))
            {
                notifyIcon.ShowBalloonTip(1000);
            }
        }

        protected void ShowHide_NotifyMenuItemClick(object sender, EventArgs e)
        {
            ToggleVisibility();
        }

        protected void Reporter_MenuItemClick(object sender, RoutedEventArgs e)
        {
            App.CurrentApp.ShowReportWindow();
        }

        protected void Admin_MenuItemClick(object sender, RoutedEventArgs e)
        {
            App.CurrentApp.ShowAdminWindow();
        }

        protected void Settings_MenuItemClick(object sender, RoutedEventArgs e)
        {
            App.CurrentApp.ShowSettingsWindow();
        }

        protected void About_MenuItemClick(object sender, RoutedEventArgs e)
        {
            App.CurrentApp.ShowAboutWindow();
        }

        protected void Exit_MenuItemClick(object sender, RoutedEventArgs e)
        {
            App.CurrentApp.GracefulShutDown();
        }

        protected void Hide_MenuItemClick(object sender, RoutedEventArgs e)
        {
            notifyIcon.ShowBalloonTip(1000);
            Hide();
        }

        protected void Task_Click(object sender, RoutedEventArgs e)
        {
            Task t = ((Button)e.Source).DataContext as Task;
            DataModel model = DataContext as DataModel;

            if (t != null && model != null)
            {
                model.CurrentUser.StartTaskNow(t, ctrlDescription.Text);
                App.CurrentApp.Database.Set(model.CurrentUser.History);
                ctrlChangeTaskExpander.IsExpanded = false;
                ctrlDescription.Text = string.Empty;
                UpdateNotifyIcon();
            }
            else
            {
                MessageBox.Show("Error: the model or the task were null");
            }
        }

        void currentUser_TaskExecutionStarted(User sender, TaskExecutionStartedEventArgs e)
        {
            UpdateNotifyIcon();
            notifyIcon.ShowBalloonTip(1000);
        }

        protected void UpdateNotifyIcon()
        {
            User user = App.CurrentApp.CurrentModel.CurrentUser;
            string result = "TEAM's TimeTracker: No task";
            if (user != null && user.IsExecutingTask)
            {
                result = "You're " + user.CurrentTask.Name + " since " + user.CurrentTaskStartTime.ToShortTimeString();
            }
            notifyIcon.BalloonTipText = result;
            result = "TEAM's TimeTracker: " + result;
            if (result.Length >= 63)
            {
                result = result.Substring(result.Length - 63);
            }
            notifyIcon.Text = result;
        }

        public void UpdateCurrentProjectsView()
        {
            currentProjectsView.Filter = new Predicate<object>(
                delegate(object item)
                {
                    bool result = false;
                    Project p = item as Project;
                    if (p != null)
                    {
                        result = p.IsActive;
                    }
                    return result;
                });
        }

    }
}